package com.sunday.android.fangdaifu.bukong.activity.index;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.adapter.IndexItemAdapter;
import com.sunday.android.fangdaifu.bukong.entity.IndexProjectDetail;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.DividerDecoration;
import com.sunday.android.fangdaifu.bukong.utils.RecyclerItemDivider;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 项目详情
* */
public class IndexItemActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.tvCount)
    TextView tvCount;
    @Bind(R.id.tvEvr)
    TextView tvEvr;
    @Bind(R.id.tvDown)
    TextView tvDown;
    @Bind(R.id.tvBreak)
    TextView tvBreak;
    @Bind(R.id.tvTime)
    TextView tvTime;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private long id;
    private LinearLayoutManager layoutManager;
    private IndexItemAdapter adapter;
    private IndexProjectDetail detail;
    private List<IndexProjectDetail.ListBean> beanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index_item);
        ButterKnife.bind(this);
        id = getIntent().getLongExtra("id",0);
        initView();
        getData();
    }

    private void initView() {
        titleView.setText(getIntent().getStringExtra("title"));
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new IndexItemAdapter(mContext,beanList);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    private void showData(){
        tvEvr.setText(String.format("倾斜 %d",detail.getHorizontal()));
        tvDown.setText(String.format("沉降 %d",detail.getSettlement()));
        tvBreak.setText(String.format("裂缝(位移) %d",detail.getCrack()));
        tvTime.setText(String.format("项目周期  %s 至 %s",detail.getStartTime(),detail.getEndTime()));
        adapter.notifyDataSetChanged();
    }

    private void getData() {
        loading();
        Call<ResultDO<IndexProjectDetail>> call = ApiClient.getApiAdapter().AcProjectDetail(id);
        call.enqueue(new Callback<ResultDO<IndexProjectDetail>>() {
            @Override
            public void onResponse(Call<ResultDO<IndexProjectDetail>> call, Response<ResultDO<IndexProjectDetail>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    detail = response.body().getResult();
                    beanList.clear();
                    beanList.addAll(detail.getList());
                    showData();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<IndexProjectDetail>> call, Throwable t) {
                if (isFinish) {
                    return;
                }
                refreshLayout.setRefreshing(false);
            }
        });
    }

    private void loading(){
        if (!refreshLayout.isRefreshing()){
            refreshLayout.setRefreshing(true);
        }
    }

    private int pageNo = 1, pageSize = 10;
    private boolean isCanloadMore = false;
    private int lastVisibleItem;

    private void handleRecycle() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                getData();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE &&
                        lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    pageNo++;
                    getData();
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                isCanloadMore = dy > 0;
            }
        });
    }
}
